package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.StrategyCatalog;
import cn.wolfcode.wolf2w.query.QueryObject;
import cn.wolfcode.wolf2w.query.StrategyQuery;
import cn.wolfcode.wolf2w.redis.service.IStrategyRedisService;
import cn.wolfcode.wolf2w.repository.StrategyRepository;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IStrategyCatalogService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.IStrategyThemeService;
import cn.wolfcode.wolf2w.util.PageUtils;
import cn.wolfcode.wolf2w.redis.vo.StrategyStatisVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class StrategyServiceImpl implements IStrategyService {

	@Autowired
	private StrategyRepository strategyRepository;
	@Autowired
	private MongoTemplate template;
	@Autowired
	private IDestinationService destinationService;
	@Autowired
	private IStrategyCatalogService strategyCatalogService;
	@Autowired
	private IStrategyThemeService strategyThemeService;
	@Autowired
	private IStrategyRedisService strategyRedisService;
	@Override
	public void save(Strategy strategy) {
		strategy.setId(null);
		strategyRepository.save(strategy);
	}

	@Override
	public void update(Strategy strategy) {
		strategyRepository.save(strategy);
	}

	@Override
	public void delete(String id) {
		strategyRepository.deleteById(id);
	}

	@Override
	public Strategy get(String id) {
		return strategyRepository.findById(id).orElse(null);
	}

	@Override
	public List<Strategy> list() {
		return strategyRepository.findAll();
	}

	@Override
	public void saveOrUpdate(Strategy strategy) {
		// 根据攻略分类id查找 攻略分类
		StrategyCatalog strategyCatalog = strategyCatalogService.get(strategy.getCatalogId());
		// 设置目的地id
		strategy.setDestId(strategyCatalog.getDestId());
		// 设置目的地name
		strategy.setDestName(strategyCatalog.getDestName());
		// 吐司导航
		List<Destination> destinations = destinationService.queryParent(new ArrayList<>(),strategy.getDestId());
		// 拿到第一个 中国
		Destination destination = destinations.get(0);
		// 判断是否是国内
		if("中国".equals(destination.getParentName())){
			strategy.setIsabroad(Strategy.ABROAD_NO);
		}else {
			strategy.setIsabroad(Strategy.ABROAD_YES);
		}
		// 设置攻略分类名称
		strategy.setCatalogName(strategyCatalog.getName());
		// 设置攻略主题名称
		strategy.setThemeName(strategyThemeService.get(strategy.getThemeId()).getName());

		if(StringUtils.hasLength(strategy.getId())){

			Strategy temp = this.get(strategy.getId());
			// 设置浏览人数,转发,收藏,分享,点赞,发布时间
			strategy.setViewnum(temp.getViewnum());
			strategy.setReplynum(temp.getReplynum());
			strategy.setFavornum(temp.getFavornum());
			strategy.setSharenum(temp.getSharenum());
			strategy.setThumbsupnum(temp.getThumbsupnum());
			strategy.setCreateTime(temp.getCreateTime());
			this.update(strategy);
		}else {
			// 新增时设置发布时间
			strategy.setCreateTime(new Date());
			this.save(strategy);
		}
	}

	@Override
	public List<Strategy> queryByViewnum(String destId) {
		Query query = new Query();
		Pageable pageable;
		if(StringUtils.hasLength(destId)){
			pageable = PageRequest.of(0,3,Sort.Direction.DESC,"viewnum");
			query.addCriteria(Criteria.where("destId").is(destId));
		}else {
			pageable = PageRequest.of(0, 3, Sort.by(Sort.Direction.DESC, "viewnum"));
		}
		query.with(pageable);
		return template.find(query, Strategy.class);
	}

	@Override
	public Page<Strategy> query(QueryObject qo) {
		Query query = new Query();

		return PageUtils.page(template,query,Strategy.class, qo,Sort.by(Sort.Direction.ASC,"_id"));
	}

	@Override
	public Page<Strategy> queryByDestId(StrategyQuery qo) {
		Query query = new Query();
		if(StringUtils.hasLength(qo.getDestId())){
			query.addCriteria(Criteria.where("destId").is(qo.getDestId()));
		}

		if(StringUtils.hasLength(qo.getThemeId())){
			query.addCriteria(Criteria.where("themeId").is(qo.getThemeId()));
		}
		// 只查询发布的
		query.addCriteria(Criteria.where("state").is(Strategy.STATE_PUBLISH));

		return PageUtils.page(template, query, Strategy.class, qo,null);
	}

	@Override
	public void updateStatus(String id,int state) {
		Strategy strategy = this.get(id);
		strategy.setState(state);
		this.update(strategy);
	}

	@Override
	public StrategyStatisVo statisVo(String sid) {
		strategyRedisService.viewnumIncreas(sid);
		return strategyRedisService.getStrategyStatisVo(sid);
	}

	@Override
	public boolean strategyFavor(String sid, String uid) {
		return strategyRedisService.favornumIncreas(sid,uid);
	}
	// 获取某个用户的收藏列表
	@Override
	public List<String> getStrategiesFavor(String userId) {
		return strategyRedisService.getUserFavorList(userId);
	}

	@Override
	public boolean strategyThumbup(String sid, String uid) {
		return strategyRedisService.strategyThumbup(sid,uid);
	}

	@Override
	public void updateStrategyVo(StrategyStatisVo vo) {
		Strategy strategy = this.get(vo.getStrategyId());
		BeanUtils.copyProperties(vo, strategy);
		this.update(strategy);
	}

	@Override
	public List<Strategy> findByDestName(String destName) {
		return strategyRepository.findByDestName(destName);
	}
}
